package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.gov.constant.examsys.TypeEnum;
import com.hyt.it.ogt.kq.service.gov.mapper.TaskRoomTestReportMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskRoomTestReport;
import com.hyt.it.ogt.kq.service.gov.model.entity.TaskRoomTestReportDetail;
import com.hyt.it.ogt.kq.service.gov.model.param.TestReportResultParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.TestReportResultDetailVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.TestReportResultVO;
import com.hyt.it.ogt.kq.service.gov.model.xml.examsys.gce.GceTestReport;
import com.hyt.it.ogt.kq.service.gov.model.xml.examsys.obt.ObtTestReport;
import com.hyt.it.ogt.kq.service.gov.service.ITaskConfigService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskRoomTestReportDetailService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskRoomTestReportService;
import com.hyt.it.ogt.kq.common.gov.utils.XmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * <p>
 * 考场测试报告表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-08-13
 */
@Service
public class TaskRoomTestReportServiceImpl extends BaseServiceImpl<TaskRoomTestReportMapper, TaskRoomTestReport> implements ITaskRoomTestReportService {

    @Resource
    private ITaskRoomTestReportDetailService taskRoomTestReportDetailService;

    @Resource
    private ITaskConfigService iTaskConfigService;

    @Override
    public TaskRoomTestReport getByTaskRoom(String taskId, String roomId) {
        QueryWrapper<TaskRoomTestReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TaskRoomTestReport::getTaskId, taskId)
                .eq(TaskRoomTestReport::getRoomId, roomId)
                .eq(TaskRoomTestReport::getDelFlag, false)
                .last("LIMIT 1");
        return getOne(queryWrapper);
    }

    @Override
    public Page<TestReportResultVO> pageList(Page<TestReportResultParam> pageParam, TestReportResultParam param) {
        return this.baseMapper.page(pageParam, param);
    }

    @Override
    public TestReportResultDetailVO detail(String testReportId) {
        //获取测试报告基本信息
        TestReportResultDetailVO testReportResultDetailVO = this.baseMapper.getRoomInfoById(testReportId);
        //获取测试报告详情
        Optional<TaskRoomTestReportDetail> taskRoomTestReportDetailOptional = taskRoomTestReportDetailService.lambdaQuery()
                .eq(TaskRoomTestReportDetail::getTestReportId, testReportId)
                .eq(TaskRoomTestReportDetail::getDelFlag, false)
                .last("LIMIT 1")
                .oneOpt();
        //组装测试报告详情数据
        return Optional.ofNullable(testReportResultDetailVO).map(detailVO -> {
            String content = taskRoomTestReportDetailOptional
                    .map(TaskRoomTestReportDetail::getContent)
                    .orElse(StringUtils.EMPTY);
            if (StringUtils.isNotBlank(content)) {
                //区分不同考试系统类型来组装测试报告详情数据
                Optional<String> optional = iTaskConfigService.getExamSystemTypeByTaskId(testReportResultDetailVO.getTaskId());
                optional.ifPresent(type -> {
                    switch (TypeEnum.getType(type)) {
                        //组装GCE考试系统测试报告详情数据
                        case GCE:
                            this.convertGceTestReportResultDetail(detailVO, content);
                            break;
                        //组装OBT考试系统测试报告详情数据
                        case OBT:
                            this.convertObtTestReportResultDetail(detailVO, content);
                            break;
                    }
                });
            }
            return detailVO;
        }).orElseGet(TestReportResultDetailVO::new);
    }

    /**
     * 组装GCE考试系统测试报告详情数据
     * @param testReportResultDetailVO 测试报告详情数据
     * @param content 测试报告详情
     */
    private void convertGceTestReportResultDetail(TestReportResultDetailVO testReportResultDetailVO, String content) {
        GceTestReport gceTestReport = XmlUtil.toObject(content, GceTestReport.class);
        testReportResultDetailVO.setPlaceName(gceTestReport.getGceRoomInfoReport().getPlaceName())
                .setRoomName(gceTestReport.getGceRoomInfoReport().getRoomName())
                .setOs(gceTestReport.getGceMonitorReport().getOperatingSystem())
                .setCpu(gceTestReport.getGceMonitorReport().getCpu())
                .setMemory(gceTestReport.getGceMonitorReport().getMemory())
                .setDiskLeavings(gceTestReport.getGceMonitorReport().getDiskFreeSpace())
                .setNetSpreed(gceTestReport.getGceMonitorReport().getNetworkSpeed())
                .setDiskSpaces(gceTestReport.getGceMonitorReport().getDiskSpace())
                .setComputerCount(gceTestReport.getGceClientReport().getClientTestCount() == null ?
                        0: Integer.parseInt(gceTestReport.getGceClientReport().getClientTestCount()))
                .setOkCount(gceTestReport.getGceClientReport().getClientPassCount() == null ? 0:
                        Integer.parseInt(gceTestReport.getGceClientReport().getClientPassCount()))
                .setStartTime(gceTestReport.getGceTestInfoReport().getStartTime())
                .setEndTime(gceTestReport.getGceTestInfoReport().getEndTime())
                .setSeatCount(gceTestReport.getGceTestInfoReport().getClientCount() == null? 0:
                        Integer.parseInt(gceTestReport.getGceTestInfoReport().getClientCount()))
                .setLoginCount(gceTestReport.getGceTestInfoReport().getLoginCount() == null? 0 :
                        Integer.parseInt(gceTestReport.getGceTestInfoReport().getLoginCount()));
    }

    /**
     * 组装OBT考试系统测试报告详情数据
     * @param testReportResultDetailVO 测试报告详情数据
     * @param content 测试报告详情
     */
    private void convertObtTestReportResultDetail(TestReportResultDetailVO testReportResultDetailVO, String content) {
        ObtTestReport obtTestReport = XmlUtil.toObject(content, ObtTestReport.class);
        testReportResultDetailVO.setPlaceName(obtTestReport.getObtRoomInfoReport().getPlaceName())
                .setRoomName(obtTestReport.getObtRoomInfoReport().getRoomName())
                .setOs(obtTestReport.getObtMonitorReport().getOperatingSystem())
                .setCpu(obtTestReport.getObtMonitorReport().getCpu())
                .setMemory(obtTestReport.getObtMonitorReport().getMemory())
                .setDiskLeavings(obtTestReport.getObtMonitorReport().getDiskFreeSpace())
                .setNetSpreed(obtTestReport.getObtMonitorReport().getNetworkSpeed())
                .setDiskSpaces(obtTestReport.getObtMonitorReport().getDiskSpace())
                .setComputerCount(obtTestReport.getObtClientReport().getClientTestNumber() == null ?
                        0: Integer.parseInt(obtTestReport.getObtClientReport().getClientTestNumber()))
                .setOkCount(obtTestReport.getObtClientReport().getClientPassNumber() == null ? 0:
                        Integer.parseInt(obtTestReport.getObtClientReport().getClientPassNumber()))
                .setStartTime(obtTestReport.getObtTestInfoReport().getStartTime())
                .setEndTime(obtTestReport.getObtTestInfoReport().getEndTime())
                .setSeatCount(obtTestReport.getObtTestInfoReport().getClientCount() == null? 0:
                        Integer.parseInt(obtTestReport.getObtTestInfoReport().getClientCount()))
                .setLoginCount(obtTestReport.getObtTestInfoReport().getLoginCount() == null? 0 :
                        Integer.parseInt(obtTestReport.getObtTestInfoReport().getLoginCount()));
    }

}
